package com.chenfan.process.entity.po;

import com.baomidou.mybatisplus.annotation.TableName;
import com.chenfan.ccp.util.tool.UuidUtil;
import com.chenfan.infra.context.UserContextHolder;
import com.chenfan.process.enums.BooleanEnum;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;

import java.util.Date;

/**
 * @author liuming
 * @date 2021/11/24
 */
@Data
@TableName("process_form")
public class ProcessForm {

    /**
     * id
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;
    /**
     * 租户id
     */
    private Long tenantId;

    /**
     * 表单名称
     */
    private String formName;

    /**
     * 创建人
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private Long createBy;

    /**
     * 创建人名称
     */
    private String createName;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    /**
     * 更新人
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private Long updateBy;

    /**
     * 更新人名称
     */
    private String updateName;

    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updateTime;

    /**
     * 状态
     */
    private Integer status;

    /**
     * 版本
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private Long version;

    /**
     * 是否删除
     */
    private Integer deletedFlag;

    /**
     * 图标
     */
    private String icon;

    /**
     * 内容, 将所有的数据保存为json结构
     */
    private String content;

    @Data
    public static class Constants {
        public static final String FORM_NAME = "form_name";

        private Constants() {}
    }

    public static ProcessForm ofDefault(String formName) {
        ProcessForm processForm = new ProcessForm();
        processForm.setId(UuidUtil.generateId());
        processForm.setFormName(formName);
        processForm.setCreateBy(UserContextHolder.getUserVo().getUserId());
        processForm.setCreateName(UserContextHolder.getUserVo().getRealName());
        return processForm;
    }

    public void updateSelf() {
        setUpdateTime(new Date());
        setUpdateBy(UserContextHolder.getUserVo().getUserId());
        setUpdateName(UserContextHolder.getUserVo().getRealName());
    }

    public boolean isAvailable() {
        return BooleanEnum.Y.getCode().equals(deletedFlag) && BooleanEnum.Y.getCode().equals(status);
    }
}
